// JavaScript Document
function jslogin(contextPath, url){
	if(!checkUserPwd()){
		return false;
	}

	var isOk = false;
	var num = 0;
	while (!isOk&&num<3){
		$.ajax({
		   url: contextPath + '/j_security_check',
		   async: false,
		   type: 'POST',
		   data: {
				   j_username: $("input[name='j_username']").val(),
			   		j_password: $("input[name='j_password']").val()
		   		},
		   		complete:function(XMLHttpRequest, textStatus){
		   			num++;
					//alert(XMLHttpRequest.responseText); 
				 },
			   success: function(msg){
			   	window.location.href = url;
			   			/*
						if(msg.indexOf("Login failure!") == -1){
						   isOk = true;
						   window.location.href = url;
					   }else{
						   isOk = true;
		  					alert("用户名或者密码有误，请检查!");
					   }
					   */
					}
		   });
	}
}

function jslogin_result(contextPath, url){
	if(!checkUserPwd()){
		return false;
	}

	var isSuccess = true;
	var isOk = false;
	var num = 0;
	while (!isOk&&num<3){
		$.ajax({
		   url: contextPath + '/j_security_check',
		   async: false,
		   type: 'POST',
		   data: {
				   j_username: $("input[name='j_username']").val(),
			   		j_password: $("input[name='j_password']").val()
		   		},
		   		complete:function(XMLHttpRequest, textStatus){
		   			num++;
					//alert(XMLHttpRequest.responseText); 
				 },
			   success: function(msg){
			   		// window.location.href = url;
		   			
					if(msg.indexOf("Login failure!") == -1){
						isSuccess = true;
					   	isOk = true;
					   	window.location.href = url;
				   }else{
				   		isSuccess = false;
					   	isOk = true;
	  					alert("用户名或者密码有误，请检查!");
				   }
				   
				}
		   });
	}
	return isSuccess;
}
  
  function checkUserPwd(){
    var userName=$("input[name='j_username']").val();
    var pwd=$("input[name='j_password']").val();
    var re=/[']|["]/;

    if(userName==null || userName==""){
       alert("用户名不能为空!");
      $("input[name='j_username']").focus();
      return false;

    }

    if(re.test(userName)){
    	alert("用户名不能含有'或者\"号");
        $("input[name='j_username']").focus();
    	return false;
    }
    if(re.test(pwd)){
    	alert("密码中不能含有'或者\"号");
        $("input[name='j_password']").focus();
    	return false;
    }
	return true;
  }
  
function logout(mainServlet, url){
	$.ajax({
		   url: mainServlet,
		   async: false,
		   type: 'POST',
		   data: {},
		   		complete:function(XMLHttpRequest, textStatus){
					//alert(XMLHttpRequest.responseText); 
				 },
			   success: function(msg){
						if(msg == 1){
							if(url != null && url != ""){
								window.location.href = url;
							}else{
								window.location.href = '/drivess/admin/index.do';
							}
						}
					}
		   });	
} 